package tasks;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import main.Constants;
import main.MyFileBody;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;


public class UploadTask implements Taskable {

	private String relPath;
	private String absPath;
	private String filename;
	private String game;
	private long timestamp;
	private long size;
	private String session;
	
	private int executeTry;

	public UploadTask(String absPath, String relPath, String filename, 
			String game, long timestamp, long size) {
		this.relPath = relPath;
		this.absPath = absPath;
		this.filename = filename;
		this.game = game;
		this.timestamp = timestamp;
		this.size = size;
		this.executeTry = 1;
		this.session = Constants.SESSION;
	}

	@Override
	public int start(Reportable context) {
		executeTry++;
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(Constants.HOST_URL+"upload.php");
		try {
			MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
			File f = new File(absPath+relPath+filename);
			if(f.exists()&&f.canRead()){
				//request
				entity.addPart("file", new MyFileBody(f, context, game, 
						relPath+filename));
				entity.addPart("game", new StringBody(game));
				entity.addPart("session", new StringBody(session));
				entity.addPart("path", new StringBody(relPath));
				entity.addPart("time", new StringBody(""+timestamp));
				post.setEntity(entity);
				
				//response
				HttpResponse res = client.execute(post);
				HttpEntity rEntity = res.getEntity();
				if(rEntity!=null){
					InputStream in = rEntity.getContent();
					BufferedReader rd = new BufferedReader(new InputStreamReader(in));
					String line;
					while((line=rd.readLine())!=null){
						if(line.startsWith("file added to sql")) {
							rEntity.consumeContent();
							return 0;
						} else {
							rEntity.consumeContent();
							return -1;
						}
					}
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public String toString() {
		return "[upload: "+relPath+filename+"]";
	}

	@Override
	public int getTry() {
		return executeTry;
	}

	@Override
	public long getSize() {
		return size;
	}

	@Override
	public String getPath() {
		return relPath+filename;
	}

}
